import mongoose, { Document, Schema } from 'mongoose';

// 任务接口定义
export interface ITask extends Document {
  title: string;
  description: string;
  status: string;
  priority: string;
  projectId: mongoose.Schema.Types.ObjectId;
  assignedTo: string;
  dueDate?: Date;
  createdBy: string;
  createdAt: Date;
  updatedAt: Date;
}

// 任务状态枚举
export enum TaskStatus {
  TODO = '待办',
  IN_PROGRESS = '进行中',
  REVIEW = '审核中',
  DONE = '已完成'
}

// 任务优先级枚举
export enum TaskPriority {
  LOW = '低',
  MEDIUM = '中',
  HIGH = '高',
  URGENT = '紧急'
}

// 任务Schema定义
const TaskSchema: Schema = new Schema(
  {
    title: {
      type: String,
      required: [true, '任务标题不能为空'],
      trim: true,
      maxlength: [200, '任务标题不能超过200个字符']
    },
    description: {
      type: String,
      trim: true
    },
    status: {
      type: String,
      required: true,
      enum: Object.values(TaskStatus),
      default: TaskStatus.TODO
    },
    priority: {
      type: String,
      required: true,
      enum: Object.values(TaskPriority),
      default: TaskPriority.MEDIUM
    },
    projectId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Project',
      required: [true, '任务必须关联到一个项目']
    },
    assignedTo: {
      type: String,
      required: [true, '任务必须分配给一个成员']
    },
    dueDate: {
      type: Date
    },
    createdBy: {
      type: String,
      required: [true, '创建者不能为空']
    }
  },
  {
    timestamps: true
  }
);

// 创建索引以提高查询性能
TaskSchema.index({ projectId: 1, status: 1 });
TaskSchema.index({ assignedTo: 1 });
TaskSchema.index({ priority: 1 });

// 创建并导出模型
export default mongoose.model<ITask>('Task', TaskSchema); 